﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RegistCommon
{
    public class UTool
    {
        public static bool CheckRegist(string key)
        {
            EncryptionHelper encryptionHelper = new EncryptionHelper(EncryptionKeyEnum.KeyB);
            bool flag = key != "";
            bool result;
            if (flag)
            {
                if (key.Length < 80)
                {
                    //MessageBox.Show("注册码不正确，请输入正确的注册码!");
                    return false;
                }
                string text = key.Substring(0, 80);
                string str = key.Substring(80);
                string a = encryptionHelper.DecryptString(text);
                string computerInfo = ComputerInfo.GetComputerInfo();
                EncryptionHelper encryptionHelper2 = new EncryptionHelper(EncryptionKeyEnum.KeyB);
                string mD5String = encryptionHelper2.GetMD5String(computerInfo);
                string registInfo = encryptionHelper2.EncryptString(mD5String);
                if (!text.Contains(registInfo))
                {
                    MessageBox.Show("机器码不一致，请注册!");
                    return false;
                }
                string text2 = encryptionHelper.DecryptString(str);
            
         
                DateTime d = Convert.ToDateTime(text2.Split(new char[]
                {
                    '&'
                })[0]);
                double num = Convert.ToDouble(text2.Split(new char[]
                {
                    '&'
                })[1]) * 24.0;
                DateTime now = DateTime.Now;
                double totalHours = (now - d).TotalHours;
                num -= totalHours;
                bool flag2 = totalHours + 0.1 < 0.0;
                bool flag3;
                if (flag2)
                {
                    MessageBox.Show("客户端时间可能被修改！");
                    flag3 = false;
                }
                else
                {
                    bool flag4 = num > 0.0;
                    if (flag4)
                    {
                        flag3 = true;
                    }
                    else
                    {
                        MessageBox.Show("程序已过期，请重新注册！");
                        flag3 = false;
                    }
                }
                bool flag5 = a == mD5String && flag3;
                if (flag5)
                {
                    num /= 24.0;
                    string str2 = encryptionHelper2.EncryptString(now.ToString() + "&" + num.ToString());
                    RegistFileHelper.WriteRegistFile(text + str2);
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            else
            {
                result = false;
            }
            return result;
        }
    }
}
